/**
 * JDK中时间函数简单学习。
 */
package com.mc.apache.common;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

public class JDK8TimeTest {

	@Test
	public void showTime() {

		LocalDateTime localDateTime = LocalDateTime.now();
		
		//2016-05-26T21:52:25.577 同IOS_LOCAL_DATE_TIME相同。
		System.out.println(localDateTime);
		
		//2016-05-26T21:24:59.602
		String local=localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
		System.out.println(local);
		
		//2016-05-26 21:24:26 
		String local2=localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:dd"));
		System.out.println(local2);
		
		//2016年5月26日 星期四
		String loca3=localDateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
		System.out.println(loca3);

	}
	
	@Test
	public void timeZoneTest(){
		ZoneId zoneId1 = ZoneId.of("Asia/Shanghai");
		ZoneId zoneId2=ZoneId.of("America/Chicago");
		//instant是一个和时区无直接关联的数据。
		Instant instant2=Instant.now();
		ZonedDateTime zonedDateTime=instant2.atZone(zoneId2);
		//2016-05-26T08:49:05.065
		System.out.println(zonedDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
		
	}
	
	@Test
	public void setTime(){
		//2015-02-11
		LocalDate localDate = LocalDate.of(2015, Month.FEBRUARY, 11);
		System.out.println(localDate);
		
		//2016-01-11T13:33:34
		LocalDateTime localDateTime=LocalDateTime.of(2016,Month.JANUARY,11,13,33,34);
		System.out.println(localDateTime);
	}
	
	
	@Test
	public void partTime(){
		LocalDate localDate=LocalDate.now();
		int dayOfYear = localDate.getDayOfYear();
		System.out.println(dayOfYear);
	}
	
	@Test
	public void addTime(){
		LocalDate localDate=LocalDate.now();
		LocalDate localDate2=localDate.plusDays(10);
		localDate.minusDays(20);
		
		boolean flag = localDate.isBefore(localDate2);
		System.out.println(flag);
		
	  	int dayOfMonth = localDate.getDayOfMonth();
	  	System.out.println(dayOfMonth);
	}
	
	
}
